package com.qboot.rabbitmq.topic;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 订阅模型 Topic - 多个消费者
 *
 * @author Tellsea
 * @date 2023/3/7
 */
@Component
public class TopicConsumer {

    /**
     * 消费者A
     *
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = TopicConst.QUEUE_NAME, durable = "true"),
            exchange = @Exchange(
                    value = TopicConst.EXCHANGE_NAME,
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {TopicConst.ROUTING_KEY_USER}
    ))
    public void listenA(String msg) {
        System.out.println("TopicConsumer listenA 接收到消息：" + msg);
    }

    /**
     * 消费者B，key值不同，接收不到消息
     *
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = TopicConst.QUEUE_NAME, durable = "true"),
            exchange = @Exchange(
                    value = TopicConst.EXCHANGE_NAME,
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {TopicConst.ROUTING_KEY_STUDENT}
    ))
    public void listenB(String msg) {
        System.out.println("TopicConsumer listenB 接收到消息：" + msg);
    }
}
